package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallSearchHistory;
import org.linlinjava.litemall.db.mapper.LitemallSearchHistoryDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallSearchHistoryService {
    @Resource
    private LitemallSearchHistoryDao searchHistoryMapper;

    public void save(LitemallSearchHistory searchHistory) {
        searchHistory.setAddTime(LocalDateTime.now());
        searchHistory.setUpdateTime(LocalDateTime.now());
        searchHistoryMapper.insertTemplate(searchHistory,true);
        //searchHistoryMapper.insertSelective(searchHistory);
    }

    public List<LitemallSearchHistory> queryByUid(int uid) {
    	
    	LambdaQuery<LitemallSearchHistory> query = searchHistoryMapper.createLambdaQuery();
    	
    	query.andEq(LitemallSearchHistory::getUserId, uid);
    	query.andEq(LitemallSearchHistory::getDeleted, false);
    	query.distinct();
    	return query.select();
		/*
		 * LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
		 * example.or().andUserIdEqualTo(uid).andDeletedEqualTo(false);
		 * example.setDistinct(true); return
		 * searchHistoryMapper.selectByExampleSelective(example,
		 * LitemallSearchHistory.Column.keyword);
		 */
    }

    public void deleteByUid(int uid) {
    	 LitemallSearchHistory example = new LitemallSearchHistory();
    	 example.setDeleted(true);
    	LambdaQuery<LitemallSearchHistory> query = searchHistoryMapper.createLambdaQuery();
    	query.andEq(LitemallSearchHistory::getUserId, uid);
    	query.updateSelective(example);
		/*
		 * LitemallSearchHistory example = new LitemallSearchHistory();
		 * example.or().andUserIdEqualTo(uid);
		 * searchHistoryMapper.logicalDeleteByExample(example);
		 */
    }

    public List<LitemallSearchHistory> querySelective(String userId, String keyword, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallSearchHistory> query = searchHistoryMapper.createLambdaQuery();
    	
    	if (!StringUtils.isEmpty(userId)) {
    		query.andEq(LitemallSearchHistory::getUserId, userId);
        }
    	if (!StringUtils.isEmpty(keyword)) {
    		query.andLike (LitemallSearchHistory::getKeyword , "%" + keyword + "%");
        }
    	query.andEq(LitemallSearchHistory::getDeleted, false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        query.limit(page, size);
        return query.select();
		/*
		 * LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
		 * LitemallSearchHistoryExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(userId)) {
		 * criteria.andUserIdEqualTo(Integer.valueOf(userId)); } if
		 * (!StringUtils.isEmpty(keyword)) { criteria.andKeywordLike("%" + keyword +
		 * "%"); } criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * searchHistoryMapper.selectByExample(example);
		 */
    }
}
